﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Mud.Common;
using System.Text;
using System.Collections.Generic;

namespace Mud.Client.WPF
{
    public partial class WindowMain : IClientShell
    {

        private Client _client;
        private AreaView _areaView;
        private System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer();


        public WindowMain()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.

            Logger.AddSink(new FileLogSink());

            _client = new Client(this);
            _client.ClientConnected += ClientConnected;
            _client.ClientDisconnected += ClientDisconnected;
            _client.ClientNewPlayerCharacter += ClientNewCharacter;
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            _timer.Tick += _timer_Tick;

            _areaView = new AreaView(_client);
            _areaView.HorizontalAlignment = HorizontalAlignment.Stretch;
            _areaView.VerticalAlignment = VerticalAlignment.Stretch;
            LayoutRoot.Children.Add(_areaView);
            _timer.Start();

        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (_client.Connected)
                _client.Disconnect();

            base.OnClosing(e);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (_client.Connected)
            {
                if (!_client.Logout())
                    _client.Disconnect();
            }
            else
            {
                WindowConnectionSettings settings = new WindowConnectionSettings();
                if (!settings.ShowDialog().GetValueOrDefault())
                    return;

                if (_client.Connect(settings.IPAddress, settings.IPPort, settings.Username, settings.Password))
                    _client.StartPlaying();
            }
        }

        #region Client related methods

        Creature IClientShell.SelectCreature(IList<Creature> creatures)
        {
            WindowChooseCharacter form = new WindowChooseCharacter(creatures);
            if (!form.ShowDialog().GetValueOrDefault())
                return null;

            return form.Selected;
        }


        void ClientConnected()
        {
            this.btnConnect.Content = "Disconnect";
            RefreshCaption();
        }

        void ClientDisconnected()
        {
            this.btnConnect.Content = "Connect";
            RefreshCaption();
        }

        void ClientNewCharacter(Creature oldCharacter, Creature newCharacter)
        {
            RefreshCaption();
        }

        #endregion

        /// <summary>
        /// Drives the client.
        /// </summary>
        private void _timer_Tick(object sender, EventArgs e)
        {
            if (_client.Connected)
                _client.Tick();
        }

        /// <summary>
        /// Refresh the window caption.
        /// </summary>
        private void RefreshCaption()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("MudClient v ");
            builder.Append(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            builder.Append(" ");
            if (!_client.Connected)
            {
                builder.Append("- not connected -");
            }
            else
            {
                builder.Append("Connected to '");
                builder.Append(_client.ServerName);
                builder.Append("' as '");
                builder.Append(_client.UserName);
                builder.Append("'");
            }
            this.Title = builder.ToString();
        }
    }
}